package com.pat.jrgwt.shared;

import java.util.HashMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.pat.jrgwt.client.BaseMonthTableView;

public class FuturesContractsAmountView extends BaseMonthTableView
{
    protected HashMap<String, double[]> m_data = new HashMap<String, double[]>();

    private static class SingletonHolder
    {
        private final static FuturesContractsAmountView instance = new FuturesContractsAmountView();
    }
    
    private FuturesContractsAmountView()
    {
        setLabel("Contracts amount");
        m_valueFormatter = NumberFormat.getFormat("###,##0");
    }
    
    public static FuturesContractsAmountView getInstance()
    {
        return SingletonHolder.instance;
    }
    
    public void setData(HashMap<String, double[]> data)
    {
        m_data = data;
        // Re-define headers
        setHeaders();
    }
    
    @Override
    public void onYearChanged(int year)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void recalculate()
    {
//        m_arCellValues = new double[m_arRowLabels.length][m_arHeaders.length];
//        for( int column = 0; column < getDataColumns() - 1; column++ )
//        {
//            GWT.log("Key:" + m_arHeaders[column]);
//            double[] arValues = m_data.get(m_arHeaders[column]);
//            for( int month = 1; month < 13; month++ )
//            {
//                setAt(month, column + 1, arValues[month - 1]);
//            }
//        }
        m_arCellValues = new double[][] {
            { 0, 34, 10, 0, 21, 5, 0, 0, 5, 5, 0 }
            , { 0, 9, 7, 0, 15, 20, 0, 0, 5, 5, 0 }
            , { 0, 5, 1, 0, 25, 148, 0, 0, 5, 5, 0 }
            , { 0, 11, 19, 0, 1, 22, 0, 0, 5, 5, 0 }
            , { 0, 3, 10, 0, 15, 20, 0, 0, 0, 0, 0 }
            , { 0, 10, 6, 0, 15, 4, 0, 10, 0, 0, 0 }
            , { 0, 8, 2, 0, 17, 6, 5, 10, 0, 0, 0 }
            , { 2, 6, 3, 19, 9, 13, 5, 10, 0, 0, 0 }
            , { 2, 6, 2, 8, 8, 12, 5, 10, 0, 0, 0 }
            , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
            , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
            , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
        };
    }

    @Override
    protected void setHeaders()
    {
//        int total = m_data == null ? 0 : m_data.size();
//        // All exchanges name + sum
//        m_arHeaders = new String[total + 1];
//        if( total > 0 )
//        {
//            int pos = 0;
//            for( String key : m_data.keySet() )
//            {
//                m_arHeaders[pos++] = key;
//            }
//        }
//        m_arHeaders[total] = "Sum";
        m_arHeaders = new String[] {"FDAX-120", "FDAX-38", "FESX", "S&P-65", "S&P-30", "NQ", "DJ", "FGBL", "EURUSD", "YPNUSD", "Sum"};
    }

    public static HashMap<String, double[]> parseJsonData(JavaScriptObject json)
    {
        HashMap<String, double[]> data = new HashMap<String, double[]>();
        JSONObject jsonObj = new JSONObject(json);
        double[] values = null;

        // Loop through exchange names
        for( String key : jsonObj.keySet() )
        {
            JSONArray ar = jsonObj.get(key).isArray();
            values = new double[12];
            for( int i = 0; i < ar.size(); i++ )
            {
                values[i] = Double.parseDouble(ar.get(i).toString());
            }
            data.put(key, values);
        }
        return data;
    }

}
